#include <STC89C5xRC.H>
#include "delay.h"

unsigned char key_keynumber;

unsigned char key1()
{
	unsigned char keynumber=0;
	if(P31 == 0){Delayms(20);while(P31==0);Delayms(20);keynumber = 1;}
	if(P30 == 0){Delayms(20);while(P30==0);Delayms(20);keynumber = 2;}
	if(P32 == 0){Delayms(20);while(P32==0);Delayms(20);keynumber = 3;}
	if(P33 == 0){Delayms(20);while(P33==0);Delayms(20);keynumber = 4;}
	
	return keynumber;


}

unsigned char key()
{
	unsigned char temp=0;
	temp=key_keynumber;
	key_keynumber=0;
	return temp;
}
	

unsigned char key_getstate()
{
	unsigned char keynumber=0;
	if(P31 == 0){keynumber = 1;}
	if(P30 == 0){keynumber = 2;}
	if(P32 == 0){keynumber = 3;}
	if(P33 == 0){keynumber = 4;}
	
	return keynumber;


}
void key_loop()
{
	static unsigned char nowstate,laststate;
	laststate=nowstate;
	nowstate=key_getstate();
	if(laststate==1 && nowstate==0)
	{
		key_keynumber=1;
	}
	if(laststate==2 && nowstate==0)
	{
		key_keynumber=2;
	}
	if(laststate==3 && nowstate==0)
	{
		key_keynumber=3;
	}
	if(laststate==4 && nowstate==0)
	{
		key_keynumber=4;
	}
	
}